<template>
  <div>
    <Dialog
      v-model="dialogVisible"
      :title="dialogTitle"
      @closed="close"
      width="800px"
      class="enhanced-dialog"
    >
      <!-- 弹窗头部 -->
      <div class="dialog-header">
        <div class="header-icon">
          <Icon icon="ep:cpu" />
        </div>
        <div class="header-content">
          <h4 class="dialog-title">{{ dialogTitle }}</h4>
          <p class="dialog-subtitle">配置模拟支付渠道参数和状态</p>
        </div>
      </div>

      <!-- 表单内容 -->
      <div class="form-container">
        <el-form
          ref="formRef"
          :model="formData"
          :rules="formRules"
          label-width="100px"
          v-loading="formLoading"
          class="enhanced-form"
        >
          <!-- 基本配置 -->
          <div class="form-section">
            <div class="section-title">
              <Icon icon="ep:setting" class="section-icon" />
              <span>基本配置</span>
            </div>

            <el-form-item label="渠道状态" prop="status" class="form-item">
              <el-radio-group v-model="formData.status" class="status-radio-group">
                <el-radio
                  v-for="dict in getDictOptions(DICT_TYPE.COMMON_STATUS)"
                  :key="parseInt(dict.value)"
                  :label="parseInt(dict.value)"
                  class="radio-item"
                >
                  <Icon
                    :icon="parseInt(dict.value) === 0 ? 'ep:check' : 'ep:close'"
                    class="radio-icon"
                  />
                  {{ dict.label }}
                </el-radio>
              </el-radio-group>
            </el-form-item>

            <el-form-item label="备注" prop="remark" class="form-item">
              <el-input
                v-model="formData.remark"
                placeholder="请输入备注信息"
                type="textarea"
                :rows="3"
                class="enhanced-textarea"
              />
              <div class="form-tip">
                <Icon icon="ep:info-filled" class="tip-icon" />
                <span>模拟支付渠道用于测试环境，支持模拟各种支付场景</span>
              </div>
            </el-form-item>
          </div>
        </el-form>
      </div>

      <template #footer>
        <div class="dialog-footer">
          <el-button :disabled="formLoading" type="primary" @click="submitForm" class="submit-btn">
            <Icon icon="ep:check" class="btn-icon" />
            确 定
          </el-button>
          <el-button @click="dialogVisible = false" class="cancel-btn">
            <Icon icon="ep:close" class="btn-icon" />
            取 消
          </el-button>
        </div>
      </template>
    </Dialog>
  </div>
</template>

<script lang="ts" setup>
import { CommonStatusEnum } from '@/utils/constants'
import { DICT_TYPE, getDictOptions } from '@/utils/dict'
import * as ChannelApi from '@/api/pay/channel'

defineOptions({ name: 'MockChannelForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formData = ref<any>({
  appId: '',
  code: '',
  status: undefined,
  feeRate: 0,
  remark: '',
  config: {
    name: 'mock-conf'
  }
})
const formRules = {
  status: [{ required: true, message: '渠道状态不能为空', trigger: 'blur' }]
}
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (appId, code) => {
  dialogVisible.value = true
  formLoading.value = true
  resetForm(appId, code)
  // 加载数据
  try {
    const data = await ChannelApi.getChannel(appId, code)

    if (data && data.id) {
      formData.value = data
      formData.value.config = JSON.parse(data.config)
    }
    dialogTitle.value = !formData.value.id ? '创建支付渠道' : '编辑支付渠道'
  } finally {
    formLoading.value = false
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  if (!formRef) return
  const valid = await formRef.value.validate()
  if (!valid) return
  // 提交请求
  formLoading.value = true
  try {
    const data = { ...formData.value } as unknown as ChannelApi.ChannelVO
    data.config = JSON.stringify(formData.value.config)
    if (!data.id) {
      await ChannelApi.createChannel(data)
      message.success(t('common.createSuccess'))
    } else {
      await ChannelApi.updateChannel(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = (appId, code) => {
  formData.value = {
    appId: appId,
    code: code,
    status: CommonStatusEnum.ENABLE,
    remark: '',
    feeRate: 0,
    config: {
      name: 'mock-conf'
    }
  }
  formRef.value?.resetFields()
}
</script>

<style lang="scss" scoped>
// 主题色变量
$primary-color: #0052cc;
$primary-light: rgba(0, 82, 204, 0.1);
$primary-border: rgba(0, 82, 204, 0.3);
$success-color: #67c23a;
$warning-color: #e6a23c;
$danger-color: #f56c6c;
$text-primary: #303133;
$text-regular: #606266;
$text-secondary: #909399;
$text-placeholder: #c0c4cc;
$border-color: #dcdfe6;
$border-light: #e4e7ed;
$bg-color: #f5f7fa;
$shadow-light: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
$shadow-base:
  0 2px 4px rgba(0, 0, 0, 0.12),
  0 0 6px rgba(0, 0, 0, 0.04);
$border-radius: 12px;
$border-radius-small: 8px;

/* 弹窗样式 */
.enhanced-dialog {
  :deep(.el-dialog) {
    border-radius: $border-radius;
    overflow: hidden;

    @media (max-width: 768px) {
      width: 95% !important;
      margin: 0 auto;
    }
  }

  :deep(.el-dialog__header) {
    padding: 0;
    border-bottom: none;
  }

  :deep(.el-dialog__body) {
    padding: 0;
  }

  :deep(.el-dialog__footer) {
    padding: 0;
    border-top: none;
  }

  :deep(.el-dialog__headerbtn) {
    top: 16px;
    right: 16px;
    z-index: 10;

    .el-dialog__close {
      color: white;
      font-size: 18px;

      &:hover {
        color: rgba(255, 255, 255, 0.8);
      }
    }
  }
}

/* 弹窗头部 */
.dialog-header {
  display: flex;
  align-items: center;
  padding: 20px 24px;
  background: linear-gradient(135deg, $primary-color 0%, #1d4ed8 100%);
  color: white;

  @media (max-width: 768px) {
    padding: 16px 20px;
    flex-direction: column;
    gap: 8px;
    text-align: center;
  }
}

.header-icon {
  width: 40px;
  height: 40px;
  background: rgba(255, 255, 255, 0.2);
  border-radius: $border-radius-small;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 18px;
  flex-shrink: 0;

  @media (max-width: 768px) {
    width: 36px;
    height: 36px;
    font-size: 16px;
  }
}

.header-content {
  flex: 1;
  margin-left: 12px;

  @media (max-width: 768px) {
    margin-left: 0;
  }
}

.dialog-title {
  font-size: 18px;
  font-weight: 600;
  margin: 0 0 4px 0;

  @media (max-width: 768px) {
    font-size: 16px;
  }
}

.dialog-subtitle {
  font-size: 13px;
  opacity: 0.9;
  margin: 0;

  @media (max-width: 768px) {
    font-size: 12px;
  }
}

/* 表单容器 */
.form-container {
  padding: 24px;
  background: white;
  min-height: 200px;

  @media (max-width: 768px) {
    padding: 16px;
  }
}

.enhanced-form {
  .form-section {
    margin-bottom: 32px;

    &:last-child {
      margin-bottom: 0;
    }

    @media (max-width: 768px) {
      margin-bottom: 24px;
    }
  }

  .section-title {
    display: flex;
    align-items: center;
    gap: 8px;
    margin-bottom: 20px;
    padding-bottom: 12px;
    border-bottom: 2px solid $primary-light;
    font-size: 16px;
    font-weight: 600;
    color: $text-primary;

    @media (max-width: 768px) {
      font-size: 14px;
      margin-bottom: 16px;
      padding-bottom: 8px;
    }
  }

  .section-icon {
    color: $primary-color;
    font-size: 18px;

    @media (max-width: 768px) {
      font-size: 16px;
    }
  }

  .form-item {
    margin-bottom: 24px;

    @media (max-width: 768px) {
      margin-bottom: 20px;
    }

    :deep(.el-form-item__label) {
      font-weight: 500;
      color: $text-regular;

      @media (max-width: 768px) {
        width: 100px !important;
        font-size: 13px;
      }
    }

    :deep(.el-form-item__content) {
      @media (max-width: 768px) {
        margin-left: 100px !important;
      }
    }
  }
}

/* 表单控件样式 */
.enhanced-textarea {
  :deep(.el-textarea__inner) {
    border-radius: $border-radius-small;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06);
    transition: all 0.2s ease;

    &:hover {
      box-shadow: 0 4px 12px rgba(0, 82, 204, 0.1);
    }

    &.is-focus {
      box-shadow: 0 4px 12px rgba(0, 82, 204, 0.2);
    }
  }
}

/* 单选框组样式 */
.status-radio-group {
  display: flex;
  gap: 16px;
  flex-wrap: wrap;

  @media (max-width: 768px) {
    gap: 12px;
    flex-direction: column;
  }

  .radio-item {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 12px 20px;
    border: 2px solid $border-light;
    border-radius: $border-radius-small;
    transition: all 0.3s ease;
    cursor: pointer;
    background: white;
    min-width: 120px;
    justify-content: center;

    @media (max-width: 768px) {
      padding: 10px 16px;
      min-width: 100px;
    }

    &:hover {
      border-color: $primary-border;
      background: $primary-light;
    }

    :deep(.el-radio__input.is-checked) {
      .el-radio__inner {
        background: $primary-color;
        border-color: $primary-color;
      }
    }

    .radio-icon {
      font-size: 16px;
      color: $text-secondary;
      transition: all 0.3s ease;

      @media (max-width: 768px) {
        font-size: 14px;
      }
    }

    &:has(.el-radio__input.is-checked) {
      border-color: $primary-color;
      background: $primary-light;

      .radio-icon {
        color: $primary-color;
      }
    }
  }
}

/* 表单提示 */
.form-tip {
  display: flex;
  align-items: center;
  gap: 6px;
  margin-top: 8px;
  font-size: 12px;
  color: $text-secondary;
  line-height: 1.4;

  @media (max-width: 768px) {
    font-size: 11px;
    margin-top: 6px;
  }
}

.tip-icon {
  color: $primary-color;
  font-size: 14px;
  flex-shrink: 0;

  @media (max-width: 768px) {
    font-size: 12px;
  }
}

/* 弹窗底部 */
.dialog-footer {
  display: flex;
  justify-content: flex-end;
  gap: 12px;
  padding: 20px 24px;
  background: #fafbff;
  border-top: 1px solid $border-light;

  @media (max-width: 768px) {
    padding: 16px 20px;
    flex-direction: column-reverse;
    gap: 8px;
  }
}

.submit-btn {
  background: linear-gradient(135deg, $primary-color 0%, #1d4ed8 100%);
  border: none;
  border-radius: $border-radius-small;
  padding: 10px 20px;
  font-weight: 500;
  transition: all 0.3s ease;

  &:hover {
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(0, 82, 204, 0.3);
  }

  &:disabled {
    opacity: 0.6;
    transform: none;
  }

  @media (max-width: 768px) {
    width: 100%;
    padding: 12px 20px;
  }

  .btn-icon {
    margin-right: 6px;
  }
}

.cancel-btn {
  border: 2px solid $border-color;
  color: $text-regular;
  border-radius: $border-radius-small;
  padding: 10px 20px;
  font-weight: 500;
  transition: all 0.3s ease;

  &:hover {
    border-color: $primary-color;
    color: $primary-color;
    background: $primary-light;
  }

  @media (max-width: 768px) {
    width: 100%;
    padding: 12px 20px;
  }

  .btn-icon {
    margin-right: 6px;
  }
}

/* 加载状态优化 */
:deep(.el-loading-mask) {
  background: rgba(255, 255, 255, 0.9);
  backdrop-filter: blur(2px);

  .el-loading-spinner {
    .path {
      stroke: $primary-color;
    }
  }
}

/* 深色模式适配 */
@media (prefers-color-scheme: dark) {
  .form-container {
    background: #374151;
  }

  .dialog-footer {
    background: #4b5563;
    border-color: #6b7280;
  }

  .section-title {
    color: #f9fafb;
    border-color: rgba(96, 165, 250, 0.3);
  }

  .enhanced-form {
    .form-item {
      :deep(.el-form-item__label) {
        color: #d1d5db;
      }
    }
  }

  .radio-item {
    background: #4b5563;
    border-color: #6b7280;
    color: #d1d5db;

    &:hover {
      background: rgba(96, 165, 250, 0.1);
      border-color: rgba(96, 165, 250, 0.3);
    }

    &:has(.el-radio__input.is-checked) {
      border-color: #60a5fa;
      background: rgba(96, 165, 250, 0.1);

      .radio-icon {
        color: #60a5fa;
      }
    }
  }

  .form-tip {
    color: #d1d5db;
  }
}
</style>
